import axios from 'axios';
import config from "@/util/config.js";

const API_URL = config.mdUrl+'/md/supporters';

const supporterAPI = {
    // 添加供应商
    addSupporter(supporter, callback) {
        axios.post(`${API_URL}`, supporter)
            .then(res => callback(res.data))
            .catch(error => console.error('Error adding supporter: ', error));
    },
    // 删除供应商
    deleteSupporter(id, callback) {
        axios.delete(`${API_URL}/${id}`)
            .then(res => callback(res.data))
            .catch(error => console.error('Error deleting supporter: ', error));
    },
    // 更新供应商
    updateSupporter(id, supporter, callback) {
        axios.put(`${API_URL}/${id}`, supporter)
            .then(res => callback(res.data))
            .catch(error => console.error('Error updating supporter: ', error));
    },
    // 根据ID获取供应商
    getSupporterById(id, callback) {
        axios.get(`${API_URL}/${id}`)
            .then(res => callback(res.data))
            .catch(error => console.error('Error getting supporter by ID: ', error));
    },
    // 获取所有供应商
    getAllSupporters(callback) {
        axios.get(`${API_URL}`)
            .then(res => callback(res.data))
            .catch(error => console.error('Error getting all supporters: ', error));
    },
    // 根据条件查询供应商
    searchSupporters(keyword, level, score, page, size, callback) {
        axios.get(`${API_URL}/search`, {
            params: {
                keyword: keyword,
                level: level,
                score: score,
                page: page,
                size: size
            }
        })
            .then(res =>{
                // //获取总条数并给res.data加一个参数.total返回
                // axios.get(`${API_URL}/supporters/count`)
                //     .then(count => {
                //         res.data ={...res,total:count.data}
                //     })
                //     .catch(error => console.error('Error getting count: ', error));
                callback(res.data)
            })
            .catch(error => console.error('Error searching supporters: ', error));
    },
    //获取总条数
    getCount(callback) {
        axios.get(`${API_URL}/count`)
            .then(res => callback(res.data))
            .catch(error => console.error('Error getting count: ', error));
    },
    //查询供应商
    getSupporterByMat(param,page,size,matId, callback) {
        axios.request({
            url:`${API_URL}/`+page+"/"+size+"/"+matId,
            method:"get",
            params:param,
            headers:{
                "Content-Type": "application/json;charset=UTF-8"
            }
        }).then((res)=>{
            callback(res.data)
        }).catch(error => console.error('Error getting supporter: ', error));
    },
    addMatSupporter(param, callback) {
        axios.request({
            url:`${API_URL}/add`,
            method:"post",
            data:JSON.stringify(param),
            headers:{
                "Content-Type": "application/json;charset=UTF-8"
            }
        }).then((res)=>{
            callback(res.data)
        }).catch(error => console.error('Error getting supporter: ', error));

    },

    //删除供应商
    deleteMatSupporter(param, callback) {
        axios.request({
            url:`${API_URL}/del`,
            method:"delete",
            params:param,
        }).then((res)=>{
            callback(res.data)
        }).catch(error => console.error('Error getting supporter: ', error));

    },
    //修改供应数量
    updateMatSupporterCount(param, callback) {
        axios.request({
            url:`${API_URL}/update`,
            method:"put",
            data:JSON.stringify(param),
            headers:{
                "Content-Type": "application/json;charset=UTF-8"
            }
        }).then((res)=>{
            callback(res.data)
        }).catch(error => console.error('Error getting supporter: ', error));

    }
};
export default supporterAPI;